﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace Assist
{
	public class SharedParameter
	{
		public static bool InsertSharedParameter(Autodesk.Revit.DB.Document doc, BuiltInCategory ost, string groupName, Dictionary<string, ParameterType> Parameters, bool visible, bool ifModify = true)
		{
			bool result;
			try
			{
				bool flag = doc == null || string.IsNullOrEmpty(groupName) || Parameters.Count <= 0;
				if (flag)
				{
					result = false;
				}
				else
				{
					DefinitionFile definitionFile = SharedParameter.SharedParametersFile(doc);
					bool flag2 = definitionFile == null;
					if (flag2)
					{
						result = false;
					}
					else
					{
						bool flag3 = definitionFile.Groups == null;
						if (flag3)
						{
							result = false;
						}
						else
						{
							DefinitionGroup definitionGroup = definitionFile.Groups.get_Item(groupName);
							bool flag4 = definitionGroup == null;
							if (flag4)
							{
								definitionGroup = definitionFile.Groups.Create(groupName);
							}
							foreach (KeyValuePair<string, ParameterType> keyValuePair in Parameters)
							{
								Definition definition = definitionGroup.Definitions.get_Item(keyValuePair.Key);
								bool flag5 = definition == null;
								if (flag5)
								{
									definition = UnifiedModified.GetDefinition(definitionGroup, keyValuePair.Key, keyValuePair.Value, visible, ifModify);
								}
								InstanceBinding instanceBinding = SharedParameter.CreateInstanceBinding(doc, definition, ost);
								bool flag6 = instanceBinding == null;
								if (!flag6)
								{
									bool flag7 = doc.ParameterBindings.Contains(definition);
									if (flag7)
									{
										doc.ParameterBindings.ReInsert(definition, instanceBinding);
									}
									else
									{
										doc.ParameterBindings.Insert(definition, instanceBinding);
									}
								}
							}
							result = true;
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		private static DefinitionFile SharedParametersFile(Autodesk.Revit.DB.Document doc)
		{
			doc.Application.SharedParametersFilename = AssistFunc.GetAppRootPath() + "\\Data\\YArchitech Shared Parameters_CN.txt";
			return doc.Application.OpenSharedParameterFile();
		}

		private static InstanceBinding CreateInstanceBinding(Autodesk.Revit.DB.Document doc, Definition key, BuiltInCategory ost)
		{
			bool flag = doc == null || key == null;
			InstanceBinding result;
			if (flag)
			{
				result = null;
			}
			else
			{
				Category category = doc.Settings.Categories.get_Item(ost);
				bool flag2 = category == null;
				if (flag2)
				{
					result = null;
				}
				else
				{
					CategorySet categorySet = doc.Application.Create.NewCategorySet();
					BindingMap parameterBindings = doc.ParameterBindings;
					bool flag3 = parameterBindings.Contains(key);
					if (flag3)
					{
                        ElementBinding elementBinding = parameterBindings.get_Item(key) as ElementBinding;
						bool flag4 = elementBinding.GetType() != typeof(InstanceBinding);
						if (flag4)
						{
							return null;
						}
						bool flag5 = elementBinding.Categories.Contains(category);
						if (flag5)
						{
							return null;
						}
						foreach (object obj in elementBinding.Categories)
						{
							Category category2 = (Category)obj;
							categorySet.Insert(category2);
						}
					}
					categorySet.Insert(category);
					InstanceBinding instanceBinding = doc.Application.Create.NewInstanceBinding(categorySet);
					result = instanceBinding;
				}
			}
			return result;
		}
	}
}
